package com.wowangz.cms.systemmanage.duty.api;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wowangz.cms.common.pojo.AjaxDataEntity;
import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.component.datagrid.pojo.DataGrid;
import com.wowangz.cms.component.datagrid.pojo.DataResult;
import com.wowangz.cms.component.datagrid.pojo.DataStyle;
import com.wowangz.cms.component.datagrid.pojo.PangingParam;
import com.wowangz.cms.component.datagrid.utils.DataTypeConstant;
import com.wowangz.cms.component.datagrid.utils.PangingTools;
import com.wowangz.cms.dailyoperation.services.pojo.Menu;
import com.wowangz.cms.dailyoperation.services.service.MenuService;
import com.wowangz.cms.syscore.IssTransactionContainer;
import com.wowangz.cms.syscore.api.BaseController;
import com.wowangz.cms.syscore.api.JSONFactory;
import com.wowangz.cms.syscore.utils.MessageResourcesUtils;
import com.wowangz.cms.syscore.utils.ServletUtils;
import com.wowangz.cms.systemmanage.duty.pojo.Duty;
import com.wowangz.cms.systemmanage.duty.pojo.DutyMenu;
import com.wowangz.cms.systemmanage.duty.service.DutyMenuService;
import com.wowangz.cms.systemmanage.duty.service.DutyService;

@Scope("prototype")
@Controller
public class DutyCheckController extends BaseController {

	/**
	 * 打开职责复核页面
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/systemmanage/dutymanage/dutycheck/toDutyCheckList.web", method = RequestMethod.GET)
	public String toDutyCheckList(HttpServletRequest request,HttpServletResponse response, Model model){
		
		return "/WEB-INF/jsp/systemmanage/dutymanage/dutycheck/dutycheckList";		
	}
	
	/**
	 * 打开职业列表加载列表数据
	 * @param request
	 * @param response
	 * @param model
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/systemmamage/dutymanage/dutycheck/queryDutyCheckListInfoDataGrid.json")
	public void queryDutyCheckListInfoDataGrid(HttpServletRequest request, HttpServletResponse response, Model model) {

		DutyService dutyService = (DutyService) IssTransactionContainer.getInstance(DutyService.class, false);
		
		PangingParam pangingParam = null;
		DataResult dataResult = new DataResult();
		DataGrid dataGrid = null;

		try {
			pangingParam = (PangingParam) ServletUtils.getParametersToBaseEntity(request, PangingParam.class);
			HashMap<String, Object> map = (HashMap) ServletUtils.getParametersToHashMap(request);
			dataGrid = new DataGrid();
			dataGrid.setPangingParam(pangingParam);

			map.put("startrow", dataGrid.getStartrow());
			map.put("endrow", dataGrid.getEndrow());
			map.put("rows", dataGrid.getRows());
			map.put("sort", dataGrid.getSort());
			map.put("order", dataGrid.getOrder());
			map.put("statusid", SysConstant.RecordStatus.VALID);
			map.put("checkstatus", SysConstant.CheckStatus.SAVE);
			map.put("modifyuserid", userContext.getLoginUser().getId());
			long resultCount = dutyService.findResultCount(map);
			List<Duty> resultList = dutyService.findResultList(map);

			DataStyle dataStyle = new DataStyle();
			dataStyle.setPageIndex(pangingParam.getPage());
			dataStyle.setDataCount(resultCount);
			dataStyle.setDataList(resultList);

			dataStyle.addDepict("id", DataTypeConstant.LONG);
			dataStyle.addDepict("dutycode", DataTypeConstant.STRING);
			dataStyle.addDepict("dutyname", DataTypeConstant.STRING);
			dataStyle.addDepictByFunction("checkstatus", SysConstant.CheckStatus.class, "getName", new Class[] { long.class });
			dataResult = PangingTools.buildResultPagerInfo(dataStyle);

			if (pangingParam.getReportExportType() == 0) {
				response.getWriter().write(JSONFactory.toJSONString(dataResult, userContext));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 复核
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping(value = "/systemmamage/dutymanage/dutycheck/checkDuty.json", method = { RequestMethod.GET, RequestMethod.POST })
	public void checkDuty(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		DutyService dutyService = (DutyService) IssTransactionContainer.getInstance(DutyService.class, false);
		AjaxDataEntity ajaxDataEntity = new AjaxDataEntity();
		try {
			Long id = Long.valueOf(getRequest().getParameter("id"));
			Duty duty = new Duty();
			duty.setId(id);
			duty.setCheckstatus(String.valueOf(SysConstant.CheckStatus.CHECKED));
			duty.setModifytime(new Date());
			duty.setModifyuserid(getUserContext().getLoginUser().getId());
			dutyService.update(duty);
			userContext.setInfoMessage(MessageResourcesUtils.getMessage(request, "alertinfo.common.java.alert8"));
			response.getWriter().write(JSONFactory.toJSONString(ajaxDataEntity, userContext));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 跳转明细页面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/systemmamage/dutymanage/dutycheck/toViewDuty.web", method = RequestMethod.GET)
	public String toViewDuty(HttpServletRequest request, Model model) {
		MenuService menuService = (MenuService) IssTransactionContainer.getInstance(MenuService.class, false);
		DutyService dutyService = (DutyService) IssTransactionContainer.getInstance(DutyService.class, false);
		DutyMenuService dutyMenuService = (DutyMenuService) IssTransactionContainer.getInstance(DutyMenuService.class, false);
		
		String id = request.getParameter("id");
		String menu_ids = "";
		Duty duty = null;
		List<DutyMenu> dutyMenulist = null;
		HashMap<String,Object> hashMap = new HashMap<String, Object>();
		hashMap.put("id", id);
		hashMap.put("statusid", SysConstant.RecordStatus.VALID);
		try {
			duty = dutyService.findDutyById(hashMap);
			//查询该职责岗位下挂接了多少个菜单，依次将菜单ID拼接字符串
			dutyMenulist = dutyMenuService.findDutyAgencyById(hashMap);
			for(int i =0;i<dutyMenulist.size();i++){
				if("".equals(menu_ids)){
					menu_ids = String.valueOf(dutyMenulist.get(i).getMenuid());
				}else{
					menu_ids = menu_ids +","+String.valueOf(dutyMenulist.get(i).getMenuid());
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		String menuid_all = "";
		HashMap<String,Object> hashMaps = new HashMap<String, Object>();
		hashMaps.put("statusid", SysConstant.RecordStatus.VALID);
		List<Menu> list = menuService.queryMenuAll(hashMaps);
		for(Menu menu : list){
			if("".equals(menuid_all)){
				menuid_all = menuid_all+menu.getId();
			}else{
				menuid_all = menuid_all+","+menu.getId();
			}
		}
		model.addAttribute("menuid_all",menuid_all);
		model.addAttribute("duty",duty);
		model.addAttribute("menu_ids",menu_ids);
		
		return "/WEB-INF/jsp/systemmanage/dutymanage/dutycheck/dutyView";
	}
}
